/*
 * 1.进入页面首先要获取部门信息和职务信息，绑定到执行的下拉框中
 * 2.获取到用户输入的信息
 *   格式校验
 *   发送给服务器对应的请求 
 */
let customeraddModule = (function () {
	let $username = $('.username'),
		$spanusername = $('.spanusername'),
		$man = $('#man'),
		$woman = $('#woman'),
		$useremail = $('.useremail'),
		$spanuseremail = $('.spanuseremail'),
		$userphone = $('.userphone'),
		$spanuserphone = $('.spanuserphone'),
		$userqq = $('.userqq'),
		$spanuserqq= $('.spanuserqq'),
		$userweixin = $('.userweixin'),
        $spanuserweixin= $('.spanuserweixin'),
        $userType=$('.userType'),
		$userdesc = $('.userdesc'),
		$submit = $('.submit');

	let customerId = null;



	// 表单校验
	function checkName() {
		let val = $username.val().trim();
		if (val.length === 0) {
			$spanusername.html('小主，当前是必填项哦~~');
			return false;
		}
		if (!/^[\u4E00-\u9FA5]{2,10}(·[\u4E00-\u9FA5]{2,10}){0,2}$/.test(val)) {
			$spanusername.html('小主，请填写真实姓名【2~10位汉字】~~');
			return false;
		}
		$spanusername.html('');
		return true;
	}

	function checkEmail() {
		let val = $useremail.val().trim();
		if (val.length === 0) {
			$spanuseremail.html('小主，当前是必填项哦~~');
			return false;
		}
		if (!/^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(val)) {
			$spanuseremail.html('小主，请填写真实邮箱~~');
			return false;
		}
		$spanuseremail.html('');
		return true;
	}

	function checkPhone() {
		let val = $userphone.val().trim();
		if (val.length === 0) {
			$spanuserphone.html('小主，当前是必填项哦~~');
			return false;
		}
		if (!/^1\d{10}$/.test(val)) {
			$spanuserphone.html('小主，请填写真实手机号码~~');
			return false;
		}
		$spanuserphone.html('');
		return true;
    }
    function checkQQ(){ 
        let val = $userqq.val().trim()
        if(val.length===0){
            $spanuserqq.html('小主，当前是必填项哦~~')
            return  false
        }
        if( !/^[1-9][0-9]{4,9}$/.test(val)){
            $spanuserqq.html('QQ格式不正确！')
            return  false
        }
        $spanuserqq.html()
        return true
        
    }

    function checkweixin(){
        let val = $userweixin.val().trim();
		if (val.length === 0) {
			$spanuserweixin.html('小主，当前是必填项哦~~');
			return false;
		}
		if (!/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(val)) {
			$spanuserweixin.html('小主，请填写正确的微信号~~');
			return false;
		}
		$spanuserweixin.html('');
		return true;
    }

	// 点击确认提交按钮
	async function submitHandle() {
		// 重复表单校验
		if (!checkName() || !checkEmail() || !checkPhone() || !checkweixin() || !checkQQ() ) return;

		// 获取用户输入的信息
		let params = {
			name: $username.val().trim(),
			sex: $man.prop('checked') ? 0 : 1,
			email: $useremail.val().trim(),
            phone: $userphone.val().trim(),
            QQ   : $userqq.val().trim(),
            weixin:$userweixin.val().trim(),
			type: $userType.val(),
			address: $userdesc.val().trim()
        };
        console.log(params)

		// 向服务器发送请求（区分新增还是修改）
		if (customerId) {
			// 修改
			params.customerId = customerId;
			let result = await axios.post('/customer/update', params);
			if (result.code == 0) {
				alert('小主，您很棒，奴家已经成功修改了这条数据~~');
				window.location.href = "userlist.html";
				return;
			}
			alert('小主，当前网络繁忙，请您刷新后重试~~');
			return;
		}
		// 新增
		let result = await axios.post('/customer/add', params);
		if (result.code == 0) {
			alert('小主，您很棒，奴家已经成功为您新增一条数据~~');
			window.location.href = "customerlist.html";
			return;
		}
		alert('小主，当前网络繁忙，请您刷新后重试~~');
	}

	// 从服务器获取用户的基本信息，绑定在对应文本框中
	async function queryBaseInfo() {
		let result = await axios.get('/customer/info', {
			params: {
				customerId
			}
		});
		if (result.code == 0) {
			result = result.data;
			$username.val(result.name);
			result.sex == 0 ? $man.prop('checked', true) : $woman.prop('checked', true);
			$useremail.val(result.email);
			$userphone.val(result.phone);
            $userqq.val(result.QQ);
            $userweixin.val(result.weixin);
			$userType.val(result.type);
			$userdesc.val(result.address);
			return;
		}
		alert(`小主，当前要编辑的员工不存在，请查证！`);
		customerId = null;
	}

	return {
		init() {
			
            
            // 获取到传递的员工ID，从而获取员工的基本信息
           
            let params = window.location.href.queryURLParams();
            
			if (params.hasOwnProperty('id')) {
				customerId = params.id;
				queryBaseInfo();
			}

			// 文本框失去焦点的时候做表单校验
			$username.blur(checkName);
			$useremail.blur(checkEmail);
            $userphone.blur(checkPhone);
            $userqq.blur(checkQQ)
            $userweixin.blur(checkweixin)

			// 点击提交按钮
			$submit.click(submitHandle);
		}
	}
})();
customeraddModule.init();